#include <iostream>
using namespace std;
const int N = 1010;
int n, m;
int t[N], w[N];
int f[N];
int main()
{
    cin >> m >> n;
    for (int i = 1; i <= n; i++)
        cin >> t[i] >> w[i];
    for (int i = 1; i <= n; i++)
    {
        for (int j = m; j >= t[i]; j--)
        {
            f[j] = max(f[j], f[j - t[i]] + w[i]);
        }
    }
    cout << f[m] << endl;
    return 0;
}